package com.xncoding.freemarker.config;

import com.xncoding.starter.inter.GlobalProperConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cuiyi
 */
@Configuration
public class PropertiesConfig {



    public static String getProperty(final String key) {
        if (key == null || key.isEmpty()) {
            throw new IllegalArgumentException("key must not be empty.");
        }
        try {
            if (System.getSecurityManager() == null) {
                return System.getProperty(key);
            } else {
                return AccessController.doPrivileged(new PrivilegedAction<String>() {
                    @Override
                    public String run() {
                        return System.getProperty(key);
                    }
                });
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

//    @Value("${com.xncoding.freemarker.test}")
    String test="/application.properties";

    @Bean("transcodepro")
    public GlobalProperConfig transcodeProperConfig() throws Exception {
        List<String> filpaths = new ArrayList<>();
        filpaths.add(test);
        try {
            return new ConfigProperties(filpaths);
        } catch (Exception e) {
//            throw new ValidationRuntimeException(ValidationMsg.VALIDATION_ENCODING_NOT_SUPPORT);
            throw e;
        }
    }

    @Bean
    public ResourceBundleMessageSource messageSource() {

        ResourceBundleMessageSource source = new ResourceBundleMessageSource();
        source.setBasenames("/test");
        source.setUseCodeAsDefaultMessage(true);

        return source;
    }

}
